package org.battery.controller;

import org.battery.entity.Page;
import org.battery.entity.Test;
import org.battery.service.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 1t on 2017-08-16.
 */
@Controller
public class TestController extends BaseController{

    @Resource(name = "testService")
    public TestService testService;

    @RequestMapping(value = "/searchTestTime")
    @ResponseBody
    public Map<String, Object> searchTestTime() {
        Map<String, Object> map = new HashMap<String, Object>();
        Test test = new Test();

        List<Test> testTime = null;
        try {
            testTime = testService.getTestTime();
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("testTimeList", testTime);
        test = null;
        return map;
    }

    @RequestMapping(value = "/searchTestDay")
    @ResponseBody
    public Map<String, Object> searchTestDay( Test test) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Test> testDay = null;
        try {
            testDay = testService.getTestDay(test);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("testDayList", testDay);
        test = null;
        return map;
    }

    @RequestMapping(value = "/searchTestName")
    @ResponseBody
    public Map<String, Object> searchTestName(Test test) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Test> testName = null;
        try {
            testName = testService.getTestName(test);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("testNameList", test);
        test = null;
        return map;
    }

    @RequestMapping(value = "/searchTestAll")
    @ResponseBody
    public Map<String, Object> searchTestAll(Test test, String page, String rows) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page pageQuery = getPage();
        pageQuery.setCurrentPage(Integer.parseInt(page));
        pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
        pageQuery.setShowCount(Integer.parseInt(rows));

        List<Test> testListAll = null;
        try {
            testListAll = testService.getTest(test, pageQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("totalCnt", pageQuery.getTotalResult());
        map.put("totalPage", pageQuery.getTotalPage());
        map.put("testListAll", testListAll);
        test = null;
        return map;
    }

}
